<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="书本名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入书本名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版社" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入出版社"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <el-cascader style="width: 200px"
                     :props="{ value: 'id',label:'name' }"
                     v-model="queryParams.categoryId"
                     :options="categoryOptions"
                     @change="handleQueryParamsChange"></el-cascader>
      </el-form-item>
      <!--      <el-form-item label="创建时间" prop="creteTime">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.creteTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择创建时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="primary"-->
    <!--          plain-->
    <!--          icon="el-icon-plus"-->
    <!--          size="mini"-->
    <!--          @click="handleAdd"-->
    <!--          v-hasPermi="['book:book:add']"-->
    <!--        >新增</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="success"-->
    <!--          plain-->
    <!--          icon="el-icon-edit"-->
    <!--          size="mini"-->
    <!--          :disabled="single"-->
    <!--          @click="handleUpdate"-->
    <!--          v-hasPermi="['book:book:edit']"-->
    <!--        >修改</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="danger"-->
    <!--          plain-->
    <!--          icon="el-icon-delete"-->
    <!--          size="mini"-->
    <!--          :disabled="multiple"-->
    <!--          @click="handleDelete"-->
    <!--          v-hasPermi="['book:book:remove']"-->
    <!--        >删除</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="warning"-->
    <!--          plain-->
    <!--          icon="el-icon-download"-->
    <!--          size="mini"-->
    <!--          @click="handleExport"-->
    <!--          v-hasPermi="['book:book:export']"-->
    <!--        >导出</el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="bookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="序号" align="center" prop="id" />-->
      <el-table-column label="书本名字" align="center" prop="name"/>
      <el-table-column label="作者" align="center" prop="author"/>
      <el-table-column label="出版社" align="center" prop="publisher"/>
      <el-table-column label="分类" align="center" prop="category"/>
      <el-table-column label="数量" align="center" prop="count"/>
      <el-table-column label="封面" align="center" prop="cover">
        <template v-slot="scope">
          <image-preview style="width: 100px;height: 100px" :src="scope.row.cover"></image-preview>
        </template>
      </el-table-column>
      <!--      <el-table-column label="创建时间" align="center" prop="creteTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.creteTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">

          <el-button
            type="primary"
            icon="el-icon-zoom-in"
            @click="handleBorrow(scope.row)"
            slot="reference"
          >
            借阅
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="书本名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入书本名字" disabled/>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入作者" disabled/>
        </el-form-item>
        <el-form-item label="出版社" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入出版社" disabled/>
        </el-form-item>
        <el-form-item label="图书分类" prop="categoryId">
          <el-cascader style="width: 380px"
                       :props="{ value: 'id',label:'name' }"
                       v-model="form.categoryId"
                       :options="categoryOptions"
                       @change="handleChange" disabled></el-cascader>
        </el-form-item>
        <el-form-item label="数量" prop="count">
          <el-input v-model="form.count" placeholder="请输入数量" disabled/>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
<!--          <image-upload v-model="form.cover" disabled></image-upload>-->
          <image-preview v-model="form.cover" :src="form.cover" style="width: 200px;height: 200px"/>
          <!--          <el-input v-model="form.cover" placeholder="请输入封面" />-->
        </el-form-item>
        <el-form-item label="归还时间" prop="returnDate">
          <el-date-picker clearable
                          v-model="form.returnDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择归还时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="insertBorrow">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listBook, getBook, delBook, addBook, updateBook, categoryTreeSelect} from "@/api/book/book";
import {addBorrow} from "../../../api/book/borrow";

export default {
  name: "Book",
  data() {
    return {
      categoryOptions: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      bookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        author: null,
        publisher: null,
        category: null,
        count: null,
        cover: null,
        creteTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        returnDate: [
          { required: true, message: '请选择归还日期', trigger: 'blur' },
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getCategoryTree();
  },
  methods: {
    handleBorrow(val) {
      if (val.count < 1) {
        this.$modal.msgError("书本数量不足,无法借书");
        return;
      }
      getBook(val.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "借阅书本";
      });
    },
    handleQueryParamsChange(val) {
      this.queryParams.categoryId = val[val.length - 1]
      console.log(this.queryParams)
    },
    handleChange(val) {
      // console.log(val)
      this.form.categoryId = val[val.length - 1]
      console.log(this.form)
    },
    /** 查询分类下拉树结构 */
    getCategoryTree() {
      categoryTreeSelect().then(response => {
        this.categoryOptions = response.data;
      });
    },

    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listBook(this.queryParams).then(response => {
        this.bookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        author: null,
        publisher: null,
        category: null,
        count: null,
        cover: null,
        creteTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加书本";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBook(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改书本";
      });
    },
    /** 提交按钮 */
    insertBorrow() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // console.log(this.form)
          addBorrow(this.form).then(response => {
              this.$modal.msgSuccess("成功借书");
              this.open = false;
              this.getList();
            });
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function () {
        return delBook(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('book/book/export', {
        ...this.queryParams
      }, `book_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
